/*The MIT License

Copyright (c) <1/27/2011> <erick.difiore>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

package com.d4a5.mapping.core
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
	
	import mx.events.CollectionEvent;
	import mx.events.CollectionEventKind;
	
	public dynamic class MapObjectStateContainer extends Proxy implements IEventDispatcher
	{
		private var _array:Array;
		private var _hash:Dictionary;
		private var _type:int = 0;
		
		private var _eventDispatcher:EventDispatcher;
		
		public static const typeIHighlightedObject:uint = 0;
		public static const typeISelectedObject:uint = 1;
		
		public var allowMultiples:Boolean = true;
		
		public function MapObjectStateContainer( type:uint = 0 )
		{
			this._array = new Array();
			this._hash = new Dictionary();
			this._eventDispatcher = new EventDispatcher(this);
			
			if( type > 1 ){
				throw new Error( "GraphicsList: type must be 0 or 1" );
			}
			this._type = type;
			
		}
		
		public function addItem( item:Object ) : void {
			var validItem:String = (this._type) 
				? ((item is ISelectedObject && item.selectable)
					? "selected" : null )
				: ((item is IHighlightedObject && item.highlightable)
					? "highlighted" : null );
			
			if( validItem ){
				if( this.allowMultiples == false && this._array.length > 0){
					this.removeAllBut( item );
				}
				
				if( this._hash[item] is int == false ){
					this._hash[item] = this._array.length;
					this._array.push( item );
					this.dispatchEvent( new CollectionEvent( CollectionEvent.COLLECTION_CHANGE , false , false , CollectionEventKind.ADD , this._hash[item] , -1 , [ item ] ) );	
				}	
				
				item[validItem] = true;
			}
		}
		
		public function get length() : uint {
			return this._array.length;
		}
		
		public function contains( item:Object ) : Boolean {
			return ( this._hash[item] is int );
		}
		
		public function removeItem( item:Object ) : void {
			var validItem:String = (this._type) 
				? ((item is ISelectedObject && item.selectable)
					? "selected" : null )
				: ((item is IHighlightedObject && item.highlightable)
					? "highlighted" : null );
			
			
			if( validItem ){
				if( this._hash[item] is int ){
					
					var pos:int = this._array.length;
					var itemsIndex:int = this._hash[item];
					while( --pos > itemsIndex ){
						this._hash[this._array[pos]]--;
					}
					
					this._array.splice( this._hash[item] as int , 1 ); 
					this.dispatchEvent( new CollectionEvent( CollectionEvent.COLLECTION_CHANGE , false , false , CollectionEventKind.REMOVE , this._hash[item] , -1 , [ item ] ) );	
					
					this._hash[item] = null;
					delete this._hash[item];
				}	
				
				item[validItem] = false;
			}
		}
		
		public function removeAll() : void {
			var length:int = this._array.length;
			for( var i:int = 0; i < length ; i++ ){
				var item:Object = this._array[i];
				
				if( this._type == 0 && item is IHighlightedObject ){
					(item as IHighlightedObject).highlighted = false;
				}
				else if( this._type == 1 && item is ISelectedObject ){
					(item as ISelectedObject).selected = false;
				}
				
				this._hash[item] = false;
			}
			
			var temp:Array = this._array.splice( 0 , this._array.length ); 
			this.dispatchEvent( new CollectionEvent( CollectionEvent.COLLECTION_CHANGE , false , false , CollectionEventKind.RESET , -1 , -1 , temp ) );
		}
		
		public function removeAllBut( save:Object ) : void {
			var foundSave:Boolean = false;
			
			var length:int = this._array.length;
			for( var i:int = 0; i < length ; i++ ){
				var item:Object = this._array[i];
				if( item == save ){
					foundSave = true;
				}
				else{
					if( this._type == 0 && item is IHighlightedObject ){
						(item as IHighlightedObject).highlighted = false;
					}
					else if( this._type == 1 && item is ISelectedObject ){
						(item as ISelectedObject).selected = false;
					}
					
					this._hash[item] = false;
				}
			}
			
			var temp:Array = this._array.splice( 0 , this._array.length ); 
			if( foundSave ){
				this._array.push( temp.splice( this._hash[save] , 1 ) );
				this._hash[save] = 0;
				if( this._array.length > 1 ){
					this.dispatchEvent( new CollectionEvent( CollectionEvent.COLLECTION_CHANGE , false , false , CollectionEventKind.REMOVE , -1 , -1 , temp ) );
				}
			}
			else{
				this.dispatchEvent( new CollectionEvent( CollectionEvent.COLLECTION_CHANGE , false , false , CollectionEventKind.RESET , -1 , -1 , temp ) );
			}
		}
		
		override flash_proxy function getProperty(name:*):* {
			return this._array[name];
		}
		
		
		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			this._eventDispatcher.addEventListener( type , listener , useCapture , priority , useWeakReference );
		}
		
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			this._eventDispatcher.removeEventListener( type , listener , useCapture );
		}
		
		public function dispatchEvent(event:Event):Boolean
		{
			return this._eventDispatcher.dispatchEvent( event );
		}
		
		public function hasEventListener(type:String):Boolean
		{
			return this._eventDispatcher.hasEventListener( type );
		}
		
		public function willTrigger(type:String):Boolean
		{
			return this._eventDispatcher.willTrigger( type );
		}
	}
}